package leetcode.day;

// 1629. 按键持续时间最长的键
public class SlowestKey {
    public static void main(String[] args) {
        int[] data = {9, 29, 49, 50};


        System.out.println(slowestKey(data, "cbcd"));
    }

    public static char slowestKey(int[] releaseTimes, String keysPressed) {
        int max = releaseTimes[0];
        int index = 0;
        char res = keysPressed.charAt(0);
        for (int i = 1; i < releaseTimes.length; i++) {
            int x = releaseTimes[i] - releaseTimes[i - 1];
            if (x > max) {
                max = x;
                index = i;
                res = keysPressed.charAt(index);
            } else if (x == max) {
                if (keysPressed.charAt(index) < keysPressed.charAt(i)) {
                    index = i;
                    res = keysPressed.charAt(i);
                }
            }
        }
        return res;
    }


    public static char slowestKey2(int[] releaseTimes, String keysPressed) {
        int max = releaseTimes[0];
        int index = 0;
        for (int i = 1; i < releaseTimes.length; i++) {
            int x = releaseTimes[i] - releaseTimes[i - 1];
            if (x > max) {
                max = x;
                index = i;

            } else if (x == max && keysPressed.charAt(index) < keysPressed.charAt(i)) {
                index = i;
            }
        }
        return keysPressed.charAt(index);
    }
}
